package com.haizhi.crm.biz.service.impl;

import com.haizhi.crm.biz.constant.MessageSourceType;
import com.haizhi.crm.biz.constant.MessageStatus;
import com.haizhi.crm.biz.dao.MessageClientDao;
import com.haizhi.crm.biz.dao.MessageUserDao;
import com.haizhi.crm.biz.dao.MessageUserResourceDao;
import com.haizhi.crm.biz.model.MessageClientDo;
import com.haizhi.crm.biz.model.MessageUserDo;
import com.haizhi.crm.biz.model.MessageUserQo;
import com.haizhi.crm.biz.model.MessageVo;
import com.haizhi.crm.biz.service.MessageUserService;
import com.haizhi.crm.common.util.PageRequestBuilder;
import com.haizhi.crm.sys.message.dao.MessageCategoryDao;
import com.haizhi.crm.sys.message.service.MessageCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * Created by wangshan on 2018/5/16.
 */
@Service
public class MessageUserServiceImpl implements MessageUserService {

    public static final Long ALL = -1l;

    @Autowired
    private MessageUserDao messageUserDao;

    @Autowired
    private MessageUserResourceDao messageUserResourceDao;

    @Autowired
    private MessageClientDao messageClientDao;

    @Autowired
    private MessageCategoryDao messageCategoryDao;

    @Autowired
    private MessageCategoryService messageCategoryService;


    @Override
    public MessageUserDo save(MessageUserDo messageUserDo) {
        return messageUserDao.save(messageUserDo);
    }

    @Override
    public Page<MessageVo> queryPage(MessageUserQo messageUserQo) {
        PageRequest pageRequest = PageRequestBuilder.get(messageUserQo);
        if (messageUserQo.getMessageCategoryDoList().size() == 0 || messageUserQo.getMessageStatuses().size() == 0 || messageUserQo.getMessageSourceTypes().size() == 0) {
            return null;
        }
        if (messageUserQo.getMessageSourceTypes().size() == 1 && messageUserQo.getMessageSourceTypes().get(0) == MessageSourceType.ALL) {
            messageUserQo.setMessageSourceTypes(Arrays.asList(MessageSourceType.values()));
        }
        if (messageUserQo.getMessageStatuses().size() == 1 && messageUserQo.getMessageStatuses().get(0) == MessageStatus.ALL) {
            messageUserQo.setMessageStatuses(Arrays.asList(MessageStatus.values()));
        }
        if (messageUserQo.getMessageCategoryDoList().size() == 1 && messageUserQo.getMessageCategoryDoList().get(0) == ALL) {
            List<Long> messageCategorys = messageCategoryDao.findIdByLeaf();
            if (messageCategorys != null && messageCategorys.size() != 0) {
                messageUserQo.setMessageCategoryDoList(messageCategorys);
            }
        }
        Page<MessageVo> messageVos = messageUserDao.findMessage(messageUserQo.getUserId(), messageUserQo.getMessageStatuses(), messageUserQo.getMessageSourceTypes(), messageUserQo.getMessageCategoryDoList(), pageRequest);
        for (MessageVo messageVo : messageVos.getContent()) {
            messageVo.setParentType(messageCategoryService.findParentTypeById(messageVo.getMsgCategoryId()).getName());
            List<MessageClientDo> messageClientDos = messageClientDao.findByMessageId(messageVo.getId());
            messageVo.setClients(messageClientDos);
        }
        return messageVos;
    }

    @Override
    public void deleteByMessageId(List<MessageUserDo> messageUserDos) {
        for (MessageUserDo messageUserDo : messageUserDos) {
            //TODO delete messageUserResource
//            messageUserResourceDao.deleteByMessageUserId(messageUserDo.getId());
            messageUserDao.deleteByUserIdAndAndMsgId(messageUserDo.getUserId(), messageUserDo.getMsgId());
        }
    }

    @Override
    public void updateStatusByMessageId(List<MessageUserDo> messageUserDos) {
        for (MessageUserDo messageUserDo : messageUserDos) {
            messageUserDao.updateMsgUserStatus(messageUserDo.getUserId(), messageUserDo.getMsgId(), MessageStatus.READ.value());
        }

    }
}
